package com.itheima.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.dto.DishDto;
import com.itheima.mapper.DishFlavorMapper;
import com.itheima.mapper.DishMapper;
import com.itheima.pojo.Dish;
import com.itheima.pojo.DishFlavor;
import com.itheima.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description: test
 * @Create by: JJ菜菜
 * @Date: 2021/11/1 16:58
 */
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Autowired
    private DishMapper dishMapper;

    @Override
    public Boolean saveDish(DishDto dishDto) {
        // 先添加dish
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDto, dish);
        dishMapper.insert(dish);
        // 再添加dish表对应的口味表
        List<DishFlavor> flavors = dishDto.getFlavors();
        // 获取dish的id
        Long dishId = dish.getId();
        if (flavors != null){
            for (DishFlavor flavor : flavors) {
                // 给口味表的外键设置dish的主键id
                flavor.setDishId(dishId);
                dishFlavorMapper.insert(flavor);
            }
        }
        return true;
    }
}
